package com.review.Test.ConcurrentUtil;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Semaphore;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import com.review.Test.User;

public class UseSemaphore1 implements Runnable {
	
	private static AtomicInteger num = new AtomicInteger();
	private static Semaphore semaphore = new Semaphore(5);

	public static void main(String[] args) throws InterruptedException {
		
		ThreadPoolExecutor pool = new ThreadPoolExecutor(10, 20, 60, TimeUnit.SECONDS, 
				new ArrayBlockingQueue<Runnable>(5));
		
		for (int i = 0; i < 20; i++) {
			pool.execute(new UseSemaphore1());
		}
		
		pool.shutdown();

	}

	@Override
	public void run() {
		try {
			semaphore.acquire();
			System.out.println(num.incrementAndGet() + "...");
			Thread.sleep(3000);
			semaphore.release();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

	}
}

